package com.luo.bbs.ums.webapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luo.bbs.ums.service.IUserInfoService;
import com.luo.bbs.ums.webapi.mapper.UserMapper;

import com.luo.bbs.ums.webapi.security.UserLoginPrincipal;
import com.luo.common.exception.BBSServiceException;
import com.luo.common.restful.ResponseCode;
import com.luo.pojo.ums.entity.User;

import com.luo.pojo.ums.vo.UserInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserInfoVO getUserInfo(String authToken) {
        UserInfoVO userInfo = new UserInfoVO();
        log.debug("请求头：" + authToken);
        //从上下文中获取用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserLoginPrincipal principal = (UserLoginPrincipal) authentication.getPrincipal();
        if (principal == null) {
            throw new BBSServiceException(ResponseCode.UNAUTHORIZED, "没有登录！");
        }
        //根据id查询完整的用户信息
        Integer id = principal.getId();
        User user = userMapper.selectById(id);
        BeanUtils.copyProperties(user, userInfo);
        return userInfo;
    }
}
